package com.hskn.hss.module.permissionRole.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hskn.hss.entity.AjaxResult;
import com.hskn.hss.exception.HssException;
import com.hskn.hss.module.permissionRole.entity.SysPermissionRole;
import com.hskn.hss.module.permissionRole.mapper.SysPermissionRoleMapper;
import com.hskn.hss.module.permissionRole.service.ISysPermissionRoleService;
import com.hskn.hss.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hskn
 * @since 2021-04-30
 */
@Service
public class SysPermissionRoleServiceImpl extends ServiceImpl<SysPermissionRoleMapper, SysPermissionRole> implements ISysPermissionRoleService {
    @Autowired
    SysPermissionRoleMapper sysPermissionRoleMapper;

    @Override
    public AjaxResult insert(JSONObject params) throws HssException {
        SysPermissionRole sysPermissionRole = params.getObject("data", SysPermissionRole.class);
        sysPermissionRole.setId(StringUtils.uuid());
        int i = sysPermissionRoleMapper.insert(sysPermissionRole);
        boolean flag = i > 0 ? true : false;

        if (flag) {
            return AjaxResult.success("操作成功");
        } else {
            return AjaxResult.fail("操作失败，请稍后再试");
        }
    }

    @Override
    public AjaxResult deleteByid(JSONObject params) throws HssException {
        String id = params.getString("id");
        int i = sysPermissionRoleMapper.deleteById(id);
        boolean flag = i > 0 ? true : false;
        if (flag) {
            return AjaxResult.success("操作成功");
        } else {
            return AjaxResult.fail("操作失败，请稍后再试");
        }
    }

    @Override
    public void deleteByMap(String roldid) throws HssException {
        Map map = new HashMap();
        map.put("roleid", roldid);
        sysPermissionRoleMapper.deleteByMap(map);
    }

    @Override
    public AjaxResult update(JSONObject params) throws HssException {
        SysPermissionRole sysPermissionRole = params.getObject("data", SysPermissionRole.class);

        if (sysPermissionRole.getId() == null) {
            QueryWrapper<SysPermissionRole> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(SysPermissionRole::getRoleid, sysPermissionRole.getRoleid());
            SysPermissionRole sysPermissionRoleByMap = sysPermissionRoleMapper.selectOne(queryWrapper);
            sysPermissionRole.setId(sysPermissionRoleByMap.getId());
        }

        int i = sysPermissionRoleMapper.updateById(sysPermissionRole);
        boolean flag = i > 0 ? true : false;
        if (flag) {
            return AjaxResult.success("操作成功");
        } else {
            return AjaxResult.fail("操作失败，请稍后再试");
        }
    }


    @Override
    public SysPermissionRole getInfoById(JSONObject params) throws HssException {
        String id = params.getString("id");
        SysPermissionRole sysPermissionRole = sysPermissionRoleMapper.selectById(id);
        return sysPermissionRole;
    }

    @Override
    public List<SysPermissionRole> getListByMap(JSONObject params) throws HssException {
        Map map = new HashMap();
        map = params.getObject("data", Map.class);
        List<SysPermissionRole> PermissionRolesList = sysPermissionRoleMapper.selectByMap(map);
        return PermissionRolesList;
    }

    @Override
    public List<SysPermissionRole> getByUserId(String uid) throws HssException {
        List<SysPermissionRole> sysPermissionList = sysPermissionRoleMapper.getListByUserId(uid);
        return sysPermissionList;
    }
}
